package com.hzzc.core.controller.back;

import com.hzzc.BizException;
import com.hzzc.Result;
import com.hzzc.core.model.SSORole;
import com.hzzc.core.service.RoleService;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author zmm
 * 用户角色服务
 */
@RestController
@RequestMapping("/back")
public class BackRoleController {

    @Autowired
    private RoleService roleService;

    @GetMapping("/roles")
    public Result<List<SSORole>> users(@RequestParam("appId") Long appId, HttpServletRequest httpServletRequest) {
        return Result.success(roleService.getAppRole(appId), "");
    }

    @PostMapping("/role")
    public Result<List<SSORole>> user(@RequestBody RoleBean roleBean) throws BizException {
        roleService.addRole(roleBean.name, roleBean.appId, roleBean.msg);
        return Result.success(roleService.getAppRole(roleBean.appId), "");
    }

    @DeleteMapping("/delRole")
    public Result<List<SSORole>> delRole(@RequestParam("roleId") Long roleId, @RequestParam("appId") Long appId) throws BizException {
        roleService.delRole(roleId);
        return Result.success(roleService.getAppRole(appId), "");
    }

    @Data
    static class RoleBean {

        private String name;

        private Long appId;

        private String msg;

    }

}
